/**
 * @Description:
 * @author 若琳
 * @date 2021/8/9
*/
const {MYSQL}  = require("../../../../../../ruoling-common/src/com/ruoling/common/config/mysqlconfig.js");

module.exports = class Conversation {

    /**
     * 查看是否下线，或者强制下线
     * @param Request
     * @param response
     * @param next
     * @returns {Promise<boolean>}
     * @constructor
     */
   static async Converintercept(Request,response,next){
       let startTrans = null;
       try{
           /**
            * 查询是否 已经被强制下线，
            */
           let connect = await MYSQL().connect();
           startTrans = await connect.startTrans();

           //是否在线
           let online_data = await startTrans.query("select * from rl_sys_user_online where sys_user_sessionid = \""+Request.sessionID+"\" AND sys_user_login_state = 'on_line' AND Is_delete = 1");
           if(online_data.length == 0){

               //离线状态
               return false;
           }
           //在线状态
           let dt = new Date(online_data[0].sys_start_time_stamp);
           let start_time = dt.getTime()/1000;
           let end_time = new Date().getTime()/1000;
           let total_time = end_time-start_time;

           if(total_time >= parseInt(online_data[0].sys_expire_time)){
               //超时状态
               return false;
           }
           //正常状态
           let obj = {
               sql:"update rl_sys_user_online set sys_last_access_time = ? where sys_user_sessionid = \""+online_data[0].sys_user_sessionid+"\"",
               param:[new Date()]
           };
           await startTrans.query(obj.sql,obj.param);
           startTrans.commit();
           return true;

       }catch (e){
           startTrans.rollback();
           return false;
       }

    }
}

